package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.util.CreateOssUtil;
import com.coding.dao.OssImagesDao;
import com.coding.dao.VideoDao;
import com.coding.pojo.OssImages;
import com.coding.pojo.Video;
import com.coding.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/
@Service
public class VideoImpl implements VideoService {
    @Autowired
    VideoDao videoDao;
    @Autowired
    OssImagesDao ossImagesDao;

    @Override
    public void addVideo(Video video, BaseModel model) throws Exception {
        if (video.getVideo_url() == null) {
            model.setFailure("上传视频失败");
            return;
        }
        //传入数据库
        if (videoDao.insert(video) == 1) {
            model.setSucceed("新增视频资源成功", videoDao.selectByPrimaryKey(video.getVideo_id()));
        } else {
            model.setFailure("新增视频资源失败");
        }
    }

    @Override
    public void upVideo(Video video, BaseModel model) throws Exception {

    }

    @Override
    public void deleteVideo(Video video, BaseModel model) throws Exception {

    }

    @Override
    public void getVideo(Video video, BaseModel model) throws Exception {

    }

    @Override
    public void getVideobyid(int video_id, BaseModel baseModel) throws Exception {
        Video video = videoDao.selectByPrimaryKey(video_id);
        if (video == null) {
            baseModel.setFailure("获取视频失败");
            return;
        }
        OssImages ossImages = ossImagesDao.selectByPrimaryKey(video.getVideo_imageid());
        if (ossImages != null) {
            video.setOssImages(ossImages);
            baseModel.setSucceed("获取视频资源成功,并获得封面", video);
        } else {
            baseModel.setSucceed("获取视频资源成功，没有封面", video);
        }
    }

    @Override
    public Video setVideo(MultipartFile file) throws Exception {
        if (file == null || file.getSize() <= 0) {
            return null;
        }
        //上传图片至oss
        Video video = CreateOssUtil.createOssVideo(file);
        return video;
    }

    @Override
    public void getVideobyname(String name, BaseModel model) throws Exception {
        List<Video> videoList = videoDao.selectByName(name);
        if (videoList == null) {
            model.setFailure("获取视频失败,未查询到关于" + name + "视频");
            return;
        }
        for (Video video : videoList
        ) {
            OssImages ossImages = ossImagesDao.selectByPrimaryKey(video.getVideo_imageid());
            if (ossImages != null) {
                video.setOssImages(ossImages);
            }
        }
        model.setSucceed("获取视频资源成功,并获得封面", videoList);

    }

    @Override
    public void getVideobytag(String tag, BaseModel model) throws Exception {
        List<Video> videoList = videoDao.selectByTag(tag);
        if (videoList == null) {
            model.setFailure("获取视频失败,未查询到关于" + tag + "视频");
            return;
        }
        for (Video video : videoList
        ) {
            OssImages ossImages = ossImagesDao.selectByPrimaryKey(video.getVideo_imageid());
            if (ossImages != null) {
                video.setOssImages(ossImages);
            }
        }
        model.setSucceed("获取视频资源成功,并获得封面", videoList);

    }
}

